﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_SatHachVienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        //
        // GET: /GPS_HTCapGPLX/
        #region Index
        public ActionResult Index(ViewModelSSHV SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SSHV>(o => (SearchModel.sSHV_MA == null || o.SHV_MA.StartsWith(SearchModel.sSHV_MA))
                                        && (SearchModel.sSHV_HOTEN == null || o.SHV_HOTEN.StartsWith(SearchModel.sSHV_HOTEN))
                                        && (SearchModel.sSHV_ACTIVE == null || o.SHV_ACTIVE == SearchModel.sSHV_ACTIVE));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SSHV>();
            Int16 icount = 1;
            foreach (var currentEntity in entities)
            {
                currentEntity.STT = icount;
                model.Add(Mapper.Map<SSHV>(currentEntity));
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSSHV viewModel)
        {
            //ViewBag.lstActivedll = new SelectList(_db.GetAll<STRANGTHAI>(), "TRANGTHAI_MA", "TRANGTHAI_TEN");
            //viewModel.listTrangThai = _db.GetAll<STRANGTHAI>();
            ViewBag.checkuser = this.getCurrentUser().MA_TINHTP;
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSSHV());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSSHV model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SSHV _model = new SSHV();
                GPS.COMMON.Helpers.CopyObject<SSHV>(model, ref _model);
                _model.SHV_MA = GetValue_Indentity();

                _db.Insert<SSHV>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            // ViewBag.lstActivedll = new SelectList(_db.GetAll<STRANGTHAI>(), "TRANGTHAI_MA", "TRANGTHAI_TEN");
        }

        public string GetValue_Indentity()
        {
            string value = "";
            Int64 valueMax = _db.DbContext.SSHVs.ToList().Max(e => Convert.ToInt64(e.SHV_MA));
            value = (valueMax + 1).ToString();
            return value;
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string idSHV)
        {
            CrudModelSSHV _model = new CrudModelSSHV();
            SSHV model = _db.GetOne<SSHV>(o => o.SHV_MA == idSHV);
            GPS.COMMON.Helpers.CopyObject<CrudModelSSHV>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSSHV model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SSHV>(o => o.SHV_MA == model.SHV_MA);
                GPS.COMMON.Helpers.CopyObject<SSHV>(model, ref _model);
                _db.Update<SSHV>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSSHV view)
        {
            //ViewBag.lstActivedll = new SelectList(_db.DbContext.STRANGTHAIs.ToList(), "TRANGTHAI_MA", "TRANGTHAI_TEN", selectedValues);
        }
        #endregion

        #region Detele
        public ActionResult Delete(string idSHV)
        {   
            SSHV model = _db.GetOne<SSHV>(o => o.SHV_MA == idSHV);
            _db.DeleteItem<SSHV>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region Detail
        public ActionResult Detail(string idSHV)
        {
            CrudModelSSHV _model = new CrudModelSSHV();
            SSHV model = _db.GetOne<SSHV>(o => o.SHV_MA == idSHV);
            GPS.COMMON.Helpers.CopyObject<CrudModelSSHV>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelSSHV view)
        {
            if (view.SHV_ACTIVE == "1")
            {
                ViewBag.txtTrangThai = "Hiệu Lực";
            }
            else
            {
                ViewBag.txtTrangThai = "Không Hiệu Lực";
            }
        }
        #endregion
	}
}